package com.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.entity.Label;
import com.service.LabelService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/label")
public class LabelController {
    private static final Logger logger = LoggerFactory.getLogger(com.controller.LabelController.class);
    @Autowired
    private LabelService labelService;

    @ResponseBody
    @GetMapping("/list")
    public String LabelList(@RequestParam Integer page, @RequestParam(required=false) String labelkey, Integer size, @RequestParam(value="orderBy",defaultValue="id") String orderBy, @RequestParam(value="sortOrder",defaultValue="asc") String sortOrder) {
        Page<Label> lb = labelService.findByPageAndParams(page, size, labelkey, orderBy, sortOrder);
        List list = new ArrayList();
//        List<String> list = new JSONObject();
        for (Label items: lb.getContent()) {
            JSONObject map = new JSONObject();
            map.put("text",items.getLabelkey()+ ':'+ items.getLabelvalue());
            map.put("id",items.getId());
            map.put("key",items.getLabelkey());
            map.put("value",items.getLabelvalue());
            list.add(map);
        }
        String labellist = JSON.toJSONString(list);
        return labellist;
     }
}

//        String jsonString = JSON.toJSONString("{test:1}");
//        List<Label> list = JSON.parseObject(lb.getContent().toString(), new TypeReference<List<Label>>() {});
//             System.out.print(result);

//        return  jsonString;
//        }